<?php
namespace App\Repository\Java;

use App\Repository\Contracts\MemberInterface;
use App\Repository\Contracts\MemberJavaInterface;
use Illuminate\Support\Facades\DB;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-02-06 13:41
 *
 * @Description:
 */
class MemberRepository extends Repository implements MemberJavaInterface
{
    protected function setPrefix()
    {
        $this->prefix = 'customer/';
    }

    public function findByNumber($number)
    {
        $form['merCardNo'] = $number;
        $result = $this->post('preCustomerView', $form);

        return $result;
    }

    public function findByPhone($phone)
    {
        $form['merPhone'] = $phone;
        $result = $this->post('preCustomerView', $form);

        return $result;
    }

    public function findById($uid)
    {
        $form['id'] = $uid;
        $result = $this->post('preCustomerView', $form);

        return $result;
    }

    public function syncCreated($storeId, array $data)
    {
        $form['merPhone'] = $data['phone'];
        $form['merCardNo'] = $data['number'];
        $form['merRealName'] = $data['real_name'];
        $form['customerName'] = $data['real_name'];
        $form['platform'] = 7;
        $form['memo'] = $data['password'].$data['salt'];
        $form['stockPlaceId'] = $storeId;

        return $this->post('saveCustomer', $form);
    }

    public function syncUpdated($uid, array $data)
    {
        $form['id'] = $uid;
        $form['merPhone'] = $data['phone'];
        $form['merCardNo'] = $data['number'];
        $form['merRealName'] = $data['real_name'];
        $form['customerName'] = $data['real_name'];
        $result = $this->post('updateCustomerById', $form);

        return $result['resultType'] === 'success';
    }

    public function syncList($createdAt = 0)
    {
        $page = 1;
        $rows = 10000;
        $form['updateDate'] = $createdAt;
        $result = $this->get(env('JAVA_RESOURCE').$this->prefix.'queryCustomerByConditionPaging.jhtml?page='.$page.'&rows='.$rows, $form);
        if ($result['total'] > 0) {
            foreach ($result['rows'] as $key => $val) {
                $insert = [];
                $update = [
                    'uid' => $val['id'], 'store_id' => $val['stockPlaceId'], 'phone' => $val['merPhone'],
                    'number' => $val['merCardNo'], 'real_name' => $val['merRealName'], 'status' => $val['status'],
                    'integral' => round($val['merPoints'], 3), 'money' => round($val['merBal'], 3),
                    'password' => substr($val['memo'], 0, 32), 'salt' => substr($val['memo'], 32),
                    'created_at' => strtotime($val['createDate']), 'updated_at' => strtotime($val['updateDate'])
                ];
                if (app(MemberInterface::class)->detail($val['id'], true)) {
                    DB::table('member')->where('uid', $val['id'])->update($update);
                    continue;
                }
                $insert[] = $update;
                if (!$key%1000) {
                    DB::table('member')->insert($insert);
                } else {
                    DB::table('member')->insert($insert);
                }
            }
        }

        return  true;
    }
}